package com.ejuornal.util;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Properties;

import org.apache.commons.io.output.FileWriterWithEncoding;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;


public class VelocityUtil {
	
	public static String init(VelocityContext context, String vm) throws IOException {
	    Properties properties = new Properties();
        properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
        properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
	    VelocityEngine velocityEngine=new VelocityEngine();
        velocityEngine.init(properties);
        //实例化一个VelocityContext
        //实例化一个StringWriter
        StringWriter writer=new StringWriter();
        //从vm目录下加载hello.vm模板,在eclipse工程中该vm目录与src目录平级
        velocityEngine.mergeTemplate(vm, "utf-8", context, writer);
        writer.flush();
        writer.close();
        return writer.toString();
	}
	
	public static void init_1(VelocityContext context, String vm, String filePath) throws IOException {
	    Properties properties = new Properties();
	    properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
	    properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
        properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
	    VelocityEngine velocityEngine=new VelocityEngine();
        velocityEngine.init(properties);
        //实例化一个VelocityContext
        //实例化一个StringWriter
        File file = new File(filePath);
        Writer  writer = new FileWriterWithEncoding(file,Charset.forName("UTF-8"),false);
        //从vm目录下加载hello.vm模板,在eclipse工程中该vm目录与src目录平级
        velocityEngine.mergeTemplate(vm, "UTF-8", context, writer);
        writer.flush();
        writer.close();
	}
	
	public static String init_2(VelocityContext context, String vm) throws IOException {
	    Properties properties = new Properties();
	    properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
        properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.init(properties);
        Template t = velocityEngine.getTemplate(vm);
        Velocity.init();
        StringWriter writer = new StringWriter();
        t.merge(context, writer);
        writer.flush();
String mailContent = writer.toString();//得到邮件内容
        writer.close();
        return mailContent;
    }
}
